// pages/signIn/signIn.js
var app = getApp(); //获取应用实例
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    content: '',
    maxLength: 400,
    currentLength: 0,
    storeId:0,
    address: '', 
    latitude: 0, 
    longitude:0
  },
  inputContent: function(e) {
    this.setData({
      content: e.detail.value.substring(0, this.data.maxLength),
      currentLength: e.detail.cursor > this.data.maxLength ? this.data.maxLength : e.detail.cursor
    })
  },
  uploadImage: function(e) {
    var that = this;
    wx.chooseImage({
      count: 4, // 默认9  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success(res) {
        const tempFilePaths = res.tempFilePaths
        app.requestAPI.uploadFileApi({},tempFilePaths)
          .then(d => {
            // var filesPath = []
            for (var i in d) {
              // 返回数据源，字符串转对象，可能因为是嵌套json，需要手动转一次？
              // filesPath += d[i] + ','
              that.data.imgList.push(app.config.URI + d[i])
            }
            // filesPath = filesPath.substring(0, filesPath.length - 1)
            // console.log(filesPath)

            //显示需手动添加域名前缀
            that.setData({
              imgList: that.data.imgList
            })
          }).catch(e => {
            console.log('图片上传异常')
          });
      }
    })
  },
  delImage: function(e) {
    console.log(e)

    this.data.imgList.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      imgList: this.data.imgList
    })
  },
  saveSign: function(e) {
    var that = this;
    var params = {
      storeId: that.data.storeId,
      content: that.data.content || '门店签到',
      userId: app.config.userId,
      latitude: that.data.latitude,
      longitude: that.data.longitude,
    };
    if (this.data.imgList.length > 0){
      //提交后台需要取出域名
      let imgList = [];
      for (let item of that.data.imgList){
        imgList.push(item.toString().replace(new RegExp(app.config.URI, "g"), ""))
      } 
      params.imgs = imgList.join(',');
    }
    if (that.data.address != ''){
      params.address = that.data.address;
    }
    app.tip.modal('确定保存签到？', function(res) {
      app.requestAPI.addSign(params)
        .then(d => {
          console.log(d)
          app.tip.toast(d.msg)
        }).catch(e => {
          console.log(e)
          app.tip.toast(e.errMsg)
          app.tip.log('getIndex请求异常')
        });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        that.setData({
          latitude: latitude,
          longitude: longitude
        })
      }
    })
      this.setData({
        address: options.address,
        storeId: options.storeId
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})